package com.yn.iotxiao.Utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.yn.iotxiao.sql.entity.Record.PassRecord;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.httpclient.DefaultHttpMethodRetryHandler;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.UsernamePasswordCredentials;
import org.apache.commons.httpclient.auth.AuthScope;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.PutMethod;
import org.apache.commons.httpclient.methods.StringRequestEntity;
import org.apache.commons.httpclient.params.HttpMethodParams;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.List;

@Slf4j
public class HttpHelper {

    public static JSONObject get(String url,String loginId,String password) {
        HttpClient client = new HttpClient();

        // 设置用户名和密码
        UsernamePasswordCredentials creds = new UsernamePasswordCredentials(loginId, password);
        client.getState().setCredentials(AuthScope.ANY, creds);

        GetMethod method = new GetMethod(url);
        method.setRequestHeader("Content-type", "application/json; charset=UTF-8");
        method.setRequestHeader("Accept", "application/json; charset=UTF-8");
        method.setDoAuthentication(true);
        String strResponseData = null;
        try {
            int statusCode = client.executeMethod(method);
            byte[] responseData = method.getResponseBodyAsString().getBytes(method.getResponseCharSet());
            strResponseData = new String(responseData, "utf-8");

        } catch (IOException e) {
            throw new RuntimeException(e);
        }

        method.releaseConnection();

        JSONObject response = XMLParse.xml2Json(strResponseData);

        return  response;
    }

    public static JSONObject put(String url,String loginId,String password, Object data) {
        HttpClient client = new HttpClient();
        // 设置用户名和密码
        UsernamePasswordCredentials creds = new UsernamePasswordCredentials(loginId, password);
        client.getState().setCredentials(AuthScope.ANY, creds);


        PutMethod method = new PutMethod(url);
//        method.setRequestHeader("Content-type", "application/json; charset=UTF-8");
//        method.setRequestHeader("Accept", "application/json; charset=UTF-8");
        method.setDoAuthentication(true);

//        method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler());

        method.setRequestBody(data.toString());

//        try {
//            method.setRequestEntity(new StringRequestEntity(data.toString(), "application/json","UTF-8"));
//        } catch (UnsupportedEncodingException e) {
//            log.info("参数错误："+ e.getLocalizedMessage());
//            throw new RuntimeException(e);
//        }

        log.info("参数："+  data.toString());

        String strResponseData = null;
        try {
            int statusCode = client.executeMethod(method);
//            if (statusCode != HttpStatus.SC_OK) {
//                throw new RuntimeException("请求信息错误");
//            }
            log.info("statusCode:" + statusCode);
            byte[] responseData = method.getResponseBodyAsString().getBytes(method.getResponseCharSet());
            strResponseData = new String(responseData, "utf-8");

            log.info("返回数据：" + strResponseData);

        } catch (IOException e) {
            log.info("返回数据错误：" + e.getLocalizedMessage());
            throw new RuntimeException(e);
        }

        method.releaseConnection();

        JSONObject response = XMLParse.xml2Json(strResponseData);

        return  response;
    }


    public static Object doPostEvent(List<PassRecord> list,String url) {
        HttpClient client = new HttpClient();

        PostMethod method = new PostMethod(url);
        method.setRequestHeader("Content-type", "application/json; charset=UTF-8");
        method.setRequestBody(JsonHelper.obj2json(list));

        String strResponseData = null;
        try {
            int statusCode = client.executeMethod(method);
            log.info("statusCode:" + statusCode);
            byte[] responseData = method.getResponseBodyAsString().getBytes(method.getResponseCharSet());
            strResponseData = new String(responseData, "utf-8");

            log.info("返回数据：" + strResponseData);

        } catch (IOException e) {
            log.info("返回数据错误：" + e.getLocalizedMessage());
            throw new RuntimeException(e);
        }

        method.releaseConnection();

        return  "传递数据";
    }


}
